<?php
namespace Web\Controller\Clinic;

/**
 * 该类主要是实现用户登录
 **/
class LoginController extends BaseController {
    /**
     * 登陆
     */
    public function login(){
        if(IS_POST){
            $mobile=I('post.mobile');
            $user=M('Doctor')->where(array('mobile'=>$mobile,'status'=>0))->find();
            //判断账户类型
            if(empty($user)){
                $this->ajaxReturn(array('code'=>3,'msg'=>'用户不存在'));
            }
            //检验登录密码是否正确
            $password = think_ucenter_md5(I('post.password'), UC_AUTH_KEY);
            $password1 = $user['password'];
            if ($password1 == $password) {
                session('user_id', $user['clinic_id']);
                //得到用户信息
                $clinic = M('Clinic')->find($user['clinic_id']);
                if(empty($clinic)){
                    $this->ajaxReturn(array('code'=>2,'msg'=>'用户不存在'));
                }
                $userinfo = setUserinfo($user['id'],$clinic,2);
                if($userinfo==false){
                    $this->ajaxReturn(array('code'=>2,'msg'=>'用户不存在'));
                }
                //返回结果
                if($userinfo['login_type']==1){
                    $home_url = U('/Web/Doctor/Doctor/index');
                }else{
                    $home_url = U('/Web/Clinic/Clinic/index');
                }
                session('userinfo', serialize($userinfo));
                $this->ajaxReturn(array('code'=>0,'url'=>$home_url));
            }else{
                $this->ajaxReturn(array('code'=>1,'msg'=>'密码不正确'));
            }
        }else{
            //设置头部
            $my_header = array(
                'h_title'=>'登录',
                'h_desc'=>'百氏医药福通',
                'h_keywords'=>'百氏医药福通',
            );
            $my_header = array_merge($this->header,$my_header);
            $this->assign($my_header);
            //加载视图
            $this->display();
        }
    }

    /**
     * 注销
     */
    public function logout() {
        cookie('user_id',null);
        cookie('userinfo',null);
        session(null);
        session('[destroy]');
        $this->redirect('/Web/Clinic/Login/login');
    }
}
